//
//  MeatSearchShopViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/31.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatSearchShopViewController.h"
#import "MeatShopListViewController.h"

#import "CustomScreenMenus.h"
#import "MeatMarketApi.h"
#import "MeatSearchHeader.h"
#import "MeatSearchTableViewCell.h"


@interface MeatSearchShopViewController ()

@property (nonatomic, strong) MeatMarketApi *meatApi;
@property (nonatomic, strong) BuyMeatPartListModel *menuListModel;
@property (nonatomic, strong) PigVarietyList *pigTypeList;
@property (nonatomic, strong) MeatPriceRangeModel *priceModel;
@property (nonatomic, strong) MeatShopSortPage *model;


@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) CustomScreenMenus *screenMenu;
@property (nonatomic, strong) MeatSearchHeader *header;


@end

@implementation MeatSearchShopViewController


#pragma mark - lifecycle methods

- (void)setupUI{
    
    self.view.backgroundColor = mBackgroudColor;
    
    [self.view addSubview:self.header];
    [self.view addSubview:self.screenMenu];
}


- (void)setupLayout{
    
    _screenMenu.mj_y = self.header.bottom;
    self.tableView.mj_y = _screenMenu.bottom;
    self.tableView.height = KScreenHeight - _screenMenu.bottom;
}


#pragma mark - networking methods

- (void)initData
{
    [self requestPriceRange];
    [self requestPigVariety];
    [self requestBuyMeatPartDataWithType:@1];
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    
    MeatShopSortingParam *params = [MeatShopSortingParam new];
    params.page = page;
    params.limit = @10;
    params.tribeId = @2;
    params.dateIf = @0;
    params.pigPositionId = self.pigPositionId;
    params.memberId = kMemberId;
    [self requestPigShopSortingByParam:params];
    
}

//肉市-卖肉-部位信息
- (void)requestBuyMeatPartDataWithType:(NSNumber *)type{
    
    @weakify(self);
    [[self.meatApi getBuyMeatPartListDataWithType:type] subscribeNext:^(BuyMeatPartListModel * _Nullable menuListModel) {
        
        @strongify(self);
        self.menuListModel = [[BuyMeatPartListModel alloc] init];
        self.screenMenu.partModel = menuListModel;
    }];
}

//获取价格区间列表
- (void)requestPriceRange{
    
    @weakify(self);
    [[self.meatApi getMeatShopPriceList] subscribeNext:^(MeatPriceRangeModel * _Nullable model) {
        @strongify(self);
        self.screenMenu.priceModel = model;
    }];
}

//品种请求
- (void)requestPigVariety {
    @weakify(self);
    [[[PigMarketApi sharedInstance] getPigVariety] subscribeNext:^(PigVarietyList * _Nullable pigVarietyList) {
        @strongify(self);
        self.screenMenu.pigListModel = pigVarietyList;
    }];
}

//
- (void)requestPigShopSortingByParam:(MeatShopSortingParam *)params{
    
    @weakify(self);
    [[self.meatApi getMeatShopSortingByParams:params] subscribeNext:^(MeatShopSortModel * _Nullable model) {
        @strongify(self);
        
        if (self.isRefresh) {
            [self endRefresh];
        }
        
        self.model = model.page;

        if ([self.model.totalCount intValue] < [self.model.pageSize intValue] ) {
            [self endLoadMoreMoData];
        }else if ([self.model.totalCount intValue] == [self.model.pageSize intValue]){
            
            if ([self.model.currPage intValue] >= [self.model.totalCount intValue]) {
                [self endLoadMoreMoData];
            }else{
                [self endLoadMore];
            }
        }
        
        if (self.model.list.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    }error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}


//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}

//上拉加载更多
- (void)loadMore
{
    [super loadMore];
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    MeatShopSortingParam *params = [MeatShopSortingParam new];
    params.page = page;
    params.limit = @10;
    params.tribeId = @2;
    params.dateIf = @0;
    params.pigPositionId = self.pigPositionId;
    params.memberId = kMemberId;
    [self requestPigShopSortingByParam:params];
}


#pragma mark - tableview.datasource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.model.list.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [UIView makeViewBgColor:mBackgroudColor cornerRadius:0];
    view.frame = CGRectMake(0, 0, KScreenWidth, 10);
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MeatSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatSearchTableViewCell"];
    
    if (indexPath.section < self.model.list.count) {
        MeatShopSortList *item = self.model.list[indexPath.section];
        cell.item = item;
        
        //进入店铺Block
        @weakify(self);
        cell.dealInputShopBlock = ^{
            @strongify(self);
            MeatShopListViewController *vc = [[MeatShopListViewController alloc] init];
            vc.shopId = item.shopId;
            vc.saleType = @1;
            [self.navigationController pushViewController:vc animated:YES];
        };
    }
    
    return cell;
}


#pragma mark - tableview.delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击tableView");
}

#pragma mark - setter methods

#pragma mark - getter methods

- (void)registerTableViewCell{

    self.tableView.separatorColor = KClearColor;
    self.tableView.rowHeight = 227;
    [self.tableView registerClass:[MeatSearchTableViewCell class] forCellReuseIdentifier:@"MeatSearchTableViewCell"];
}


- (MeatMarketApi *)meatApi{
    
    if(!_meatApi){
        _meatApi = [[MeatMarketApi alloc]init];
    }
    return _meatApi;
}


- (CustomScreenMenus *)screenMenu{
    
    if (!_screenMenu) {
    
        _screenMenu = [[CustomScreenMenus alloc] init];
        _screenMenu.isHiddenNav = YES;
    }
    return _screenMenu;
}


- (MeatSearchHeader *)header{
    
    if (!_header) {
        _header = [[MeatSearchHeader alloc] init];
        
        @weakify(self);
        _header.leftNavBlock = ^{
            @strongify(self);
            [self.navigationController popViewControllerAnimated:YES];
            [self.screenMenu removeScreenView];
        };
        
    }
    return _header;
}


- (MeatPriceRangeModel *)priceModel{
    
    if (!_priceModel) {
        _priceModel = [[MeatPriceRangeModel alloc] init];
    }
    return _priceModel;
}


- (PigVarietyList *)pigTypeList{
    
    if (!_pigTypeList) {
        _pigTypeList = [PigVarietyList new];
    }
    return _pigTypeList;
}


- (MeatShopSortPage *)model{
    
    if (!_model) {
        _model = [[MeatShopSortPage alloc] init];
    }
    return _model;
}


#pragma mark - override methods

- (void)loadErrorData
{
    [super loadErrorData];
    self.screenMenu.hidden = YES;
}


- (void)loadNoData
{
    [super loadNoData];
    self.screenMenu.hidden = YES;
}


- (void)endLoadMore{
    [super endLoadMore];
    self.screenMenu.hidden = NO;
    
}

- (void)endRefresh{
    [super endRefresh];
    self.screenMenu.hidden = NO;
    
}

- (void)endLoadMoreMoData
{
    [super endLoadMoreMoData];
    self.screenMenu.hidden = NO;
}



@end
